package com.xcfw.dorm.controller.dormassistant;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.Building;
import com.xcfw.dorm.entity.Duty;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.sevice.ZSGCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: Dorm
 * @description: 值周
 * @author: Bruse Queen
 * @create: 2018-04-07 16:42
 **/
@RestController
@Slf4j
@RequestMapping(value = "/dormAssistant")
@CrossOrigin
public class DutyController extends BaseController {
    @Autowired
    @Qualifier("dutyServiceImpl")
    private ZSGCService zsgcService;

    @RequestMapping(value = "/addDuty", method = RequestMethod.POST)
    public ReturnJson addDuty(@RequestBody Duty duty) {
        try {
            zsgcService.add(duty);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "值周信息新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("DutyController---addDuty--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteDuty", method = RequestMethod.POST)
    public ReturnJson deleteDuty(@RequestBody Duty duty) {
        try {
            zsgcService.delete(duty.getId());
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "值周信息删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("DutyController---deleteDuty--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateDuty", method = RequestMethod.POST)
    public ReturnJson updateDuty(@RequestBody Duty duty) {
        try {
            zsgcService.update(duty);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "值周信息修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("DutyController---updateDuty--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectDuty", method = RequestMethod.POST)
    public ReturnJson selectDuty(@RequestBody Duty duty) {
        try {
            List<Building> list = (List<Building>) zsgcService.select(duty);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "值周信息查询成功！", list);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("DutyController---selectDuty--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }
}
